import 'package:bike_ride/common/mvvm/base_page.dart';
import 'package:bike_ride/common/util/account_manager.dart';
import 'package:bike_ride/common/util/event_bus_utils.dart';
import 'package:bike_ride/common/util/hysize_fit.dart';
import 'package:bike_ride/common/util/language.dart';
import 'package:bike_ride/common/util/phone_call.dart';
import 'package:bike_ride/common/util/sp_utils.dart';
import 'package:bike_ride/common/widget/avatar.dart';
import 'package:bike_ride/common/widget/info_row_widget.dart';
import 'package:bike_ride/common/widget/page_title_widget.dart';
import 'package:bike_ride/common/widget/picker_tool.dart';
import 'package:bike_ride/common/widget/super_button_widget.dart';
import 'package:bike_ride/model/main/model/user_info_m.dart';
import 'package:bike_ride/model/personal/view_model/personal_vm.dart';
import 'package:bike_ride/res/global/globals.dart';
import 'package:bike_ride/generated/l10n.dart';
import 'package:bike_ride/res/style/color_styles.dart';
import 'package:bike_ride/res/style/theme_styles.dart';
import 'package:bike_ride/router/navigator_util.dart';
import 'package:bike_ride/router/routers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

/// 个人资料
class PersonalView extends BaseStatefulPage<PersonalViewModel> {
  final UserInfoM? model;

  const PersonalView({this.model, super.key});

  @override
  PersonalViewState createState() => PersonalViewState();
}

class PersonalViewState
    extends BaseStatefulPageState<PersonalView, PersonalViewModel> {
  late UserInfoM? userInfo;

  @override
  void initAttribute() {
    userInfo = widget.model;
  }

  @override
  void initObserver() {}

  @override
  PersonalViewModel viewBindingViewModel() {
    return PersonalViewModel()..viewState = this;
  }

  @override
  Widget appBuild(BuildContext context) {
    final arrowForward = Icon(
      Icons.arrow_forward_ios,
      size: 32.rpx,
      color: const Color(0xFF848F99),
    );
    return Scaffold(
      extendBodyBehindAppBar: true,
      backgroundColor: ColorStyles.color_F3F4F5,
      body: Column(
        children: [
          PageTitleWidget(
            title: S.current.personInfo,
          ),
          InfoRowWidget(
            left: S.current.avatar,
            right: Avatar(
                url: userInfo?.baseInfo?.avatar,
                defaultPath: 'assets/images/default_face.png',
                onTap: () {}),
          ),
          _buildDivider(),
          InfoRowWidget(
            left: S.current.nickName,
            right: userInfo?.baseInfo?.nickname ?? S.current.defaultName,
          ),
          SizedBox(
            height: 12.rpx,
          ),
          GestureDetector(
            onTap: () {
              EasyLoading.showError(S.current.developerTips);
            },
            child: InfoRowWidget(
              left: S.current.phone,
              center: userInfo?.baseInfo?.mobile ?? '-',
              right: arrowForward,
            ),
          ),
          SizedBox(
            height: 12.rpx,
          ),
          GestureDetector(
            onTap: (){
              NavigatorUtil.push(context, Routers.protocolView);
            },
            child: InfoRowWidget(
                left: S.current.merchantAgreement,
                right: arrowForward),
          ),
          _buildDivider(),
          GestureDetector(
            onTap: () {
              PhoneCallUtil.showPhoneCallModalPopup(
                  context, AccountManager.brandInfo?.servicePhone);
            },
            child: InfoRowWidget(
                left: S.current.serverPhone,
                center: AccountManager.brandInfo?.servicePhone ?? '-'),
          ),
          _buildDivider(),
          InfoRowWidget(
              left: S.current.serverEmail,
              center: AccountManager.brandInfo?.serviceEmail ?? '-'),
          SizedBox(
            height: 12.rpx,
          ),
          GestureDetector(
            onTap: () {
              Language().showPicker(context);
            },
            child: InfoRowWidget(
                left: S.current.switchLanguage,
                center: Language.languageMap[Language().locale],right: arrowForward,),

          ),
          SizedBox(
            height: 12.rpx,
          ),
          GestureDetector(
            onTap: () {
              EasyLoading.showError(S.current.developerTips);
            },
            child: InfoRowWidget(
                left: S.current.cancelAccount, right: arrowForward),
          ),
        ],
      ),
      bottomNavigationBar: Padding(
        padding: EdgeInsets.symmetric(
            horizontal: ThemeStyles.pagePaddingHorizontal, vertical: 48.rpx),
        child: SuperButtonWidget(
          btnText: S.current.signOut,
          onPressed: () {
            viewModel!.signOut();
          },
        ),
      ),
    );
  }

  Widget _buildDivider() {
    return Padding(
      padding:
          EdgeInsets.symmetric(horizontal: ThemeStyles.pagePaddingHorizontal),
      child: Divider(
        height: 1.rpx,
        color: ColorStyles.color_E5E5E5,
      ),
    );
  }
}
